package com.study.hot.jetpack.room

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.study.hot.databinding.ActivityRoomBinding
import com.study.hot.jetpack.room.db.AppDataBase
import com.study.hot.jetpack.room.db.Money
import com.study.hot.jetpack.room.db.User
import com.study.hot.jetpack.room.one2many.IdCard
import com.study.hot.jetpack.room.one2many.Person
import org.jetbrains.anko.sdk27.coroutines.onClick


/**
 * <pre>
 *     author : fauks
 *     time   : 2020/03/25
 *     desc   :
 * </pre>
 */
class RoomActivity : AppCompatActivity() {

    lateinit var roomBinding: ActivityRoomBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        roomBinding = ActivityRoomBinding.inflate(layoutInflater)

        setContentView(roomBinding.root)

        testUserDB()

        // 一对一关联
        testOneToOne()
    }

    private fun testOneToOne() {
        val personDao = AppDataBase.getInstance(this).getPersonDao()
        val idCardDao = AppDataBase.getInstance(this).getIdCardDao()

        roomBinding.apply {
            oneOneInsert.onClick {
                val person1 = Person(1, "slx")
                val person2 = Person(2, "fauks")

                val p1Idcard = IdCard(1, "slx idcard", 1)
                val p2Idcard = IdCard(2, "fauks idcard", 2)

                personDao.insert(person1, person2)
                idCardDao.insert(p1Idcard, p2Idcard)
            }

            oneOneQuery.onClick {
                personDao.getPersonAndIdCard().observe(this@RoomActivity) {
                    result.text = "$it"
                }
            }
        }
    }

    private fun testUserDB() {
        val userDao = AppDataBase.getInstance(this).getUserDao()

        roomBinding.apply {

            // 查询全部
            userQueryAll.onClick {
                val users = userDao.queryAllUser()
                result.text = "$users"
            }

            userQueryById.onClick {
                val user = userDao.queryUserById(3L)
                result.text = "$user"
            }

            // 插入
            userInsert.onClick {
                for (i in 1..10) {
                    val user = User(i.toLong(), "用户$i", "青岛$i", "desc$i", Money(i, "工资"))
                    userDao.addUser(user)
                }
            }

            userDelete.onClick {
                userDao.deleteUser(userDao.queryUserById(5L))
            }

            userBatchDelete.onClick {
                userDao.deleteByUserIDs(listOf(2, 3, 4))
            }

            userDeleteAll.onClick {
                userDao.deleteAll()
            }

            userUpdate.onClick {
                val user = User(4L, "updateName", "updateAddress", "test", Money(100, ""))
                //user.userId = 4L
                userDao.updateUser(user)
            }

            userUpdateById.onClick {
                userDao.updateUserName("updateById", 6L)
            }
        }
    }
}